<?php
/**
 * Student Relatives Controller
 *
 * Student Base Information Management
 * student entry
 *
 * @version $Id: RelativesController.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package student
 * @subpackage Controller
 */
class student_RelativesController extends Zend_Controller_Action
{

    function indexAction()
    {
        if ($this->getRequest()->getParam('year_semester')){
       	    $this->view->year_semester = $this->getRequest()->getParam('year_semester');
       	    $this->view->class_id = $this->getRequest()->getParam('class_id');
       	}
       	else{
       	    $cookie = Zend_Controller_Request_Http::getCookie();
       	    if ( isset($cookie['Student_year_semester'])) {
       	        $this->view->year_semester = $cookie['Student_year_semester'];
       	        $this->view->class_id = $cookie['Student_class_id'];
       	    }else {
       	        $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
       	        $this->view->class_id = $this->view->lowYear.'-1';
       	    }
       	}


    }

    /**
     * 所有學生資料
     * @return unknown_type
     */
    public function getdataAction()
    {
        $this->getHelper('layout')->disableLayout();
        //$db = Zend_Db_Table::getDefaultAdapter();
        $sn = $this->getRequest()->getParam('sn');
        $this->view->sn = $sn;

        $form = new Student_Form_StudentParents();
        $form->setElementsBelongTo('parentsM');
        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentParents')
        ->where('student_sn=? AND sex=?',array($sn,'M'));
        $res = $q->fetchOne();
        if ($res)
        $form->setDefaults($res->toArray());

        $this->view->parentsM = $form;

        $form = new Student_Form_StudentParents();
        $form->setElementsBelongTo('parentsF');

        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentParents')
        ->where('student_sn=? AND sex=?',array($sn,'F'));
        $res = $q->fetchOne();
        if ($res)
        $form->setDefaults($res->toArray());
        $this->view->parentsF = $form;


        $form2 = new Student_Form_StudentGuardian();
        $form2->setElementsBelongTo('guardian');
        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentGuardian')
        ->where('student_sn=?',$sn);
        $res = $q->fetchOne();
        if ($res)
        $form2->setDefaults($res->toArray());
        $this->view->guardian = $form2;

        $form3 = new Student_Form_StudentGrandparents();
        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentGrandparents')
        ->where('student_sn=? AND sex=?',array($sn,'M'));
        $res = $q->fetchOne();
        if ($res)
        $form3->setDefaults($res->toArray());

        $form3->setElementsBelongTo('grandparentsM');
        $this->view->grandparentsM = $form3;

        $form3 = new Student_Form_StudentGrandparents();

        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentGrandparents')
        ->where('student_sn=? AND sex=?',array($sn,'F'));
        $res = $q->fetchOne();
        if ($res)
        $form3->setDefaults($res->toArray());
        $form3->setElementsBelongTo('grandparentsF');
        $this->view->grandparentsF = $form3;

    }

    public function setAction()
    {
        $request = $this->getRequest();
        $message = array();
        if ($request->isPost()){
            $update_sn = Teacher_Model_Teacher::getSnById($this->view->user->username);
            $form = new Student_Form_StudentParents();
            $data = $request->getParam('parentsM');
            $data['sex'] = 'M';
            $data['update_sn'] = $update_sn;

            if ($form->isValid($data)){
                $parentsM = new Student_Model_StudentParents();
                $q = $parentsM->getTable()->createQuery('a')->select('sn')->where('student_sn=? AND sex=?',array($data['student_sn'],$data['sex']));

                if ($q->count()>0) {
                    unset($data['sn']);
                    $tempArr = $q->fetchOne()->toArray();
                    $parentsM->assignIdentifier($tempArr['sn']);
                }

                $parentsM->fromArray($data);
                $parentsM->save();

            }
            else
            $message['parentsM'] = $form->getMessages();

            $data = $request->getParam('parentsF');
            $data['sex'] = 'F';
            $data['update_sn'] = $update_sn;
            if ($form->isValid($data)){
                $parentsF = new Student_Model_StudentParents();
                $q = $parentsF->getTable()->createQuery('a')->select('sn')->where('student_sn=? AND sex=?',array($data['student_sn'],$data['sex']));
                if ($q->count()>0) {
                    unset($data['sn']);
                    $tempArr = $q->fetchOne()->toArray();
                    $parentsF->assignIdentifier($tempArr['sn']);
                }
                $parentsF->fromArray($data);
                $parentsF->save();
            }
            else
            $message['parentsF'] = $form->getMessages();

            $form = new Student_Form_StudentGuardian();
            $data = $request->getParam('guardian');
            if ($form->isValid($data)) {
                $guardian = new Student_Model_StudentGuardian();
                $q = $guardian->getTable()->createQuery('a')->select('sn')->where('student_sn=?',$data['student_sn']);
                if ($q->count()>0) {
                    unset($data['sn']);
                    $tempArr = $q->fetchOne()->toArray();
                    $guardian->assignIdentifier($tempArr['sn']);
                }
                $guardian->fromArray($data);
                $guardian->save();
            }
            else
            $message['guardian'] = $form->getMessages();

            $form = new Student_Form_StudentGrandparents();
            $data = $request->getParam('grandparentsM');
            $data['update_sn'] = $update_sn;
            $data['sex'] = 'M';

            if ($form->isValid($data)){
                $parentsM = new Student_Model_StudentGrandparents();
                // 先找出已有資料
                $q = $parentsM->getTable()->createQuery('a')->select('sn')->where('student_sn=? AND sex=?',array($data['student_sn'],$data['sex']));
                if ($q->count()>0) {
                    unset($data['sn']);
                    $tempArr = $q->fetchOne()->toArray();
                    $parentsM->assignIdentifier($tempArr['sn']);
                }
                $parentsM->fromArray($data);
                $parentsM->save();
            }
            else
            $message['grandparentsM'] = $form->getMessages();

            $data = $request->getParam('grandparentsF');
            $data['update_sn'] = $update_sn;
            $data['sex'] = 'F';

            if ($form->isValid($data)){
                $parentsF = new Student_Model_StudentGrandparents();
                $q = $parentsF->getTable()->createQuery('a')->select('sn')->where('student_sn=? AND sex=?',array($data['student_sn'],$data['sex']));
                if ($q->count()>0) {
                    unset($data['sn']);
                    $tempArr = $q->fetchOne()->toArray();
                    $parentsF->assignIdentifier($tempArr['sn']);
                }
                $parentsF->fromArray($data);
                $parentsF->save();
            }
            else
            $message['grandparentsF'] = $form->getMessages();

        }
        if ($message)
        $this->_helper->json(array('error'=>$message));
        else
        $this->_helper->json(array('success'=>1));
    }

    function getaddressAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn');
        $student = new Student_Model_Student();
        $res = $student->getTable()->findOneBy('sn',$sn);
        $row = array();
        $row['registered_address'] = $res->registered_address;
        /*
         $db = Zend_Db_Table::getDefaultAdapter();
         $sql = $db->select()
         ->from('student',array('registered_address'))
         ->where('sn=?',$sn);
         $res = $db->query($sql)->fetch();
         */
        $this->_helper->json($row);
    }
}